package com.xian.im.modules.command.group.handler;

import cn.hutool.core.bean.BeanUtil;
import com.xian.im.common.constant.CommandCodeConstant;
import com.xian.im.common.model.dto.GroupDto;
import com.xian.im.common.proto.MsgRequest;
import com.xian.im.common.proto.MsgResponse;
import com.xian.im.common.utils.bussiness.GroupContextUtil;
import com.xian.im.common.utils.bussiness.XimResponseUtil;
import com.xian.im.common.utils.sys.SpringContextHolder;
import com.xian.im.modules.command.group.AbsGroupCommand;
import com.xian.im.modules.im.entity.GroupEntity;
import com.xian.im.modules.im.service.GroupService;
import com.xian.im.modules.message.GroupHandler;

/**
 * @program: xian-im-server
 * @description: 解除群组禁言
 * @author: liru.xian
 * @create: 2021-01-28 22:46
 **/
public class GroupOpenMute extends AbsGroupCommand {

    @Override
    public void handler(MsgRequest request, String ctx, GroupDto groupEntity) {
        groupEntity.setShutUp(0);
        SpringContextHolder.getBean(GroupService.class).updateById(BeanUtil.copyProperties(groupEntity, GroupEntity.class));
        GroupContextUtil.getSessionGroup(groupEntity.getGroupUniqueId()).setShutUp(0);
        MsgResponse response = XimResponseUtil.buildSuccess(userChannelClient().session(ctx));
        response.setBody(groupEntity.getName() + " 解除禁言");
        response.setGroupUniqueId(groupEntity.getGroupUniqueId());
        GroupHandler.groupSendAll(response, groupEntity.getGroupUniqueId());
    }

    /**
     * 群组code
     *
     * @return
     */
    @Override
    public String cmdCode() {
        return CommandCodeConstant.GROUP_OPEN_MUTE;
    }
}
